package com.myres.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.myres.model.User;

public class GetMessage extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public GetMessage() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        
		process(request,response);
	}
    private void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	//System.out.println("xml visit once");
    	response.setCharacterEncoding("gbk");
    	User user=(User)request.getSession().getAttribute("user");
    	if(user==null){
    		response.getWriter().write(-1);
    		return;
    	}
    	int uid=user.getId();
    	String s=(String)request.getSession().getAttribute("message_from_id2");
    	if(s==null) s="1";
    	int fromId=Integer.parseInt(s);
    	int[] re=new MessageService().getNewMessageCount(uid,fromId);
		int maxId=re[0];
		int newCount=re[1];
		
	    if(maxId>fromId){//has new message
	    	request.getSession().setAttribute("message_from_id2", String.valueOf(maxId));
	    	if(re[1]>0){
	    		//System.out.println("code:"+1);
	    		response.getWriter().write("1");//has unread message ,and have new message,should show icon and play music
	    	}
	    }else {
	    	if(newCount>0){
	    		//System.out.println("code:"+2);
	    		response.getWriter().write("2"); //has unread message ,but have no new message;
	    	}else {
	    		//System.out.println("code:"+3);
	    		response.getWriter().write("3");
	    	}
	    }
    }
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		process(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
